package com.edu.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.edu.bean.Cardetails;
import com.edu.bean.Comment;
import com.edu.dao.BaseDao;
import com.edu.dao.CardetailsDao;

public class CardetailsDaoImpl implements CardetailsDao {

	@Override
	public List<Cardetails> selectCommentAll(int pageNum, int pageSize) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Cardetails> clis = new ArrayList();
		String sql = "SELECT * FROM cardetails LIMIT ?,?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, (pageNum-1)*pageSize);
			ps.setInt(2, pageSize);
			res = ps.executeQuery();
			while(res.next()) {
				Cardetails car = new Cardetails();
				car.setCardetailsId(res.getInt("cardetailsId"));
				car.setCarid(res.getInt("carid"));
				car.setIntroduce(res.getString("introduce"));
				car.setEngine(res.getString("engine"));
				car.setCarkw(res.getString("carkw"));
				car.setTorque(res.getString("torque"));
				car.setAccel(res.getString("accel"));
				car.setDrivingmode(res.getString("drivingmode"));
				car.setMph(res.getString("mph"));
				clis.add(car);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return clis;
	}

	@Override
	public int selectCardetailstotal(Cardetails cardetails) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		int result = 0;
		String sql = "SELECT count(1) as total FROM cardetails WHERE 1=1 ";
		List<Object> obj = new ArrayList<Object>();
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				result = res.getInt("total");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return result;
	}

	@Override
	public int deleteCardetailsById(int id) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "DELETE FROM cardetails WHERE carDetailsId = ?";
		List<Object> obj = new ArrayList<Object>();
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id);
			result = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		
		return result;
	}

	@Override
	public int updateCardetails(Cardetails id) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "UPDATE cardetails set carId = ?,introduce = ?,engine = ?,carKw = ?,torque= ?,accel= ?,drivingMode= ?,mph= ? WHERE carDetailsId = ? ";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, id.getCarid());
			ps.setString(2, id.getIntroduce());
			ps.setString(3, id.getEngine());
			ps.setString(4, id.getCarkw());
			ps.setString(5, id.getTorque());
			ps.setString(6, id.getAccel());
			ps.setString(7, id.getDrivingmode());
			ps.setString(8, id.getMph());
			ps.setInt(9, id.getCardetailsId());
			result = ps.executeUpdate();
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

	@Override
	public Cardetails selectCardetailsById(int id) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		Cardetails cils=null;
		String sql = "SELECT * FROM cardetails WHERE carDetailsId = ?";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1,id);
			res = ps.executeQuery();
			if(res.next()) {
				cils = new Cardetails();
				cils.setCardetailsId(res.getInt("cardetailsId"));
				cils.setCarid(res.getInt("carid"));
				cils.setIntroduce(res.getString("introduce"));
				cils.setEngine(res.getString("engine"));
				cils.setCarkw(res.getString("carkw"));
				cils.setTorque(res.getString("torque"));
				cils.setAccel(res.getString("accel"));
				cils.setDrivingmode(res.getString("drivingmode"));
				cils.setMph(res.getString("mph"));
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, res);
		}
		
		return cils;
	}

	@Override
	public int addCardetails(Cardetails cardetails) {
		Connection con = null;
		PreparedStatement ps = null;
		int result = 0;
		String sql = "INSERT INTO cardetails(carId,introduce,engine,carKw,torque,accel,drivingMode,mph) VALUES (?,?,?,?,?,?,?,?)";
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1, cardetails.getCarid());
			ps.setString(2, cardetails.getIntroduce());
			ps.setString(3, cardetails.getEngine());
			ps.setString(4, cardetails.getCarkw());
			ps.setString(5, cardetails.getTorque());
			ps.setString(6, cardetails.getAccel());
			ps.setString(7, cardetails.getDrivingmode());
			ps.setString(8, cardetails.getMph());
			result = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			BaseDao.closeAll(con, ps, null);
		}
		return result;
	}

}
